let app = getApp()
import QRCode from '../../static/js/weapp-qrcode';
Page({
  data: {
    integral: 0,
    code: '',
    showCode: false
  },
  hideModal() {
    this.setData({
      showCode: false
    })
  },
  produceCode(e) {
    let {
      integral
    } = e.detail.value
    const W = wx.getSystemInfoSync().windowWidth;
    const rate = 750 / W;
    this.qrcode_w = 160 / rate;
    new QRCode('myQrcode', {
      text: JSON.stringify({
        integral: integral
      }),
      width: this.qrcode_w,
      height: this.qrcode_w,
      colorDark: '#000000',
      colorLight: '#ffffff',
      padding: 2,
      correctLevel: QRCode.CorrectLevel.L,
      callback: res => {
        console.log(res)
        console.log(this.data)
        this.setData({
          code: res.path,
          showCode: true
        })

      }
    });
  },
  saveCode() {
    var imagesUrls = this.data.code;
    const modal = {
      title: '授权',
      content: '需要您授权使用保存到相册服务',
      confirmText: '设置'
    }
    app.$util.setScope('scope.writePhotosAlbum', modal).then(res => {
      wx.getImageInfo({
        src: imagesUrls,
        success(res) {
          wx.saveImageToPhotosAlbum({
            filePath: res.path,
            success() {
              util.successToShow("保存图片成功")
            }
          })
        }
      })
    })  
  }
})